import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
// import store from '@/store'
// import { getAuthToken } from '@/utils/auth'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
// create an axios instance
const service = axios.create({
  baseURL: '/apiexam', // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 10000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    if (localStorage.getItem('Authorization')) {
      config.headers.token = localStorage.getItem('Authorization');
      config.headers.account = localStorage.getItem('account');
    }
    // const authToken = getAuthToken()
    // console.info('authToken====' + JSON.stringify(authToken))
    // if (authToken.access_token) {
    // console.info('authToken====' + JSON.stringify(authToken))
    // config.headers.Authorization = 'Bearer ' + authToken.access_token
    // }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    var urle = response.config.url
    const res = response.data
    // console.info(urle.indexOf('oauth/token'), urle.length, urle.length - urle.indexOf('oauth/token'))
    // if the custom code is not 20000, it is judged as an error.
    // 用户未登陆
    // if (res.success == false) console.log(res, res.success, res.success == false); //Message.error(res.msg);
    // if (res.code === 401) {
    //   // to re-login
    //   MessageBox.confirm('您已经登出，您可以取消停留在这个页面，或再次登录', '确认注销', {
    //     confirmButtonText: '重新登录',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     // store.dispatch('user/resetToken').then(() => {
    //     //   location.reload()
    //     // })
    //   })
    // }
    // if (res.code === 400 || res.code === 500) {
    //   // to re-login
    //   Message.error(res.msg)
    // }
    if (!res.success) {

      if (res.msg && res.msg.indexOf("登录异常") != -1) {
        MessageBox.confirm('您已经登出，您可以取消停留在这个页面，或再次登录', '确认注销', {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          window.location.href = '/#/login??url=' + location.hash
        })
      } else {
        Message({
          message: res.msg || res.data[0].defaultMessage || 'Error',
          type: 'error',
          duration: 5 * 1000
        })
      }
      return Promise.reject(new Error(res.msg || 'Error'))
    } else {
      return res
    }
  },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
